package com.mkl.ttxt.pd.controller;

import com.mkl.ttxt.common.bo.TtxtResult;
import com.mkl.ttxt.common.constant.BaseConstant;
import com.mkl.ttxt.pd.bo.Box;
import com.mkl.ttxt.pd.bo.Lineup;
import com.mkl.ttxt.pd.bo.Target;
import com.mkl.ttxt.pd.service.IPdService;
import com.mkl.ttxt.remote.controller.TtxtUserController;
import com.mkl.ttxt.remote.util.TokenUtil;
import com.mkl.ttxt.remote.util.loginUtil.UserLoginToken;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/pd")
@Api(description = "排刀管理")
public class PdController {

    @Autowired
    IPdService pdService;

    private static final Logger logger = LoggerFactory.getLogger(PdController.class);

    @RequestMapping(value = "/0.initMemberBox")
    @ResponseBody
    @ApiOperation(value = "0.初始化成员box", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult initMemberBox(@RequestParam(value = "rank") Integer rank,
                                    HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
//        pdService.initMemberBox(rank, userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/0.getMember")
    @ResponseBody
    @ApiOperation(value = "0.获取成员列表", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult getMember(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        ttxtResult.setData(pdService.getMember(userId));
        return ttxtResult;
    }

    @RequestMapping(value = "/0.getCard")
    @ResponseBody
    @ApiOperation(value = "0.获取角色列表", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult getCard(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        ttxtResult.setData(pdService.getCard());
        return ttxtResult;
    }

//    @RequestMapping(value = "/0.getMemberBox")
//    @ResponseBody
//    @ApiOperation(value = "0.获取成员box", httpMethod = "POST")
//    @UserLoginToken
//    public TtxtResult getMemberBox(HttpServletRequest httpServletRequest) {
//        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
//        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
//        ttxtResult.setData(pdService.getMemberBox(userId));
//        return ttxtResult;
//    }

    @RequestMapping(value = "/1.updateBox")
    @ResponseBody
    @ApiOperation(value = "1.修改成员box", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult updateBox(@RequestBody Box box,
                                HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.updateBox(box, userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/2.addLineup")
    @ResponseBody
    @ApiOperation(value = "2.添加阵容", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult addLineup(@RequestBody Lineup lineup,
                                HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.addLineup(lineup, userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/2.getAllLineup")
    @ResponseBody
    @ApiOperation(value = "2.获取阵容", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult getAllLineup(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        ttxtResult.setData(pdService.getAllLineup(userId));
        return ttxtResult;
    }

    @RequestMapping(value = "/3.calPossibleLineUp")
    @ResponseBody
    @ApiOperation(value = "3.计算可能的阵容", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult calPossibleLineUp(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.calPossibleLineUp(userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/4.calPossibleCompose")
    @ResponseBody
    @ApiOperation(value = "4.计算可能的阵容组合", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult calPossibleCompose(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.calPossibleCompose(userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/5.optimizePossibleCompose")
    @ResponseBody
    @ApiOperation(value = "5.自动优化组合", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult optimizePossibleCompose(@RequestParam(value = "composeNum", required = false)Integer composeNum,
                                              @RequestParam(value = "difComposeRepeat", required = false)Integer difComposeRepeat,
                                              HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.optimizePossibleCompose(userId, composeNum, difComposeRepeat);
        return ttxtResult;
    }

    @RequestMapping(value = "/5.subMemberCompose")
    @ResponseBody
    @ApiOperation(value = "5.去除某个用户的组合", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult subMemberCompose(@RequestParam(value = "memberId")Integer memberId,
                                       @RequestParam(value = "lineupComposeId")Integer lineupComposeId,
                                       HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.subMemberCompose(memberId, lineupComposeId, userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/5.getAllSetMeal")
    @ResponseBody
    @ApiOperation(value = "5.获取所有套餐详情", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult getAllSetMeal(HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        ttxtResult.setData(pdService.getAllSetMeal(userId));
        return ttxtResult;
    }

    @RequestMapping(value = "/5.getUserSetMeal")
    @ResponseBody
    @ApiOperation(value = "5.获取个人套餐", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult getUserSetMeal(@RequestParam(value = "userName", required = false)String userName,
                                     HttpServletRequest httpServletRequest) {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        if(StringUtil.isNullOrEmpty(userName)){
            ttxtResult.setData(pdService.getAllSetMeal(userId));
        }else{
            ttxtResult.setData(pdService.getUserSetMeal(userName, userId));
        }
        return ttxtResult;
    }

    @RequestMapping(value = "/6.calResult")
    @ResponseBody
    @ApiOperation(value = "6.计算排刀结果", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult calResult(@RequestBody List<Target> targetList,
                                HttpServletRequest httpServletRequest) throws Exception {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        TtxtResult ttxtResult = new TtxtResult(BaseConstant.SUCCESS);
        pdService.calResult(targetList, userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/6.calResultV2")
    @ResponseBody
    @ApiOperation(value = "6.计算排刀结果", httpMethod = "POST")
    @UserLoginToken
    public TtxtResult calResultV2(@RequestBody List<Target> targetList,
                                HttpServletRequest httpServletRequest) throws Exception {
        Integer userId = TokenUtil.explainToken(httpServletRequest.getHeader("token"));
        logger.info("使用排刀功能：" + userId);
        TtxtResult ttxtResult = pdService.calResultV2(targetList, userId);
        return ttxtResult;
    }

    @RequestMapping(value = "/7.downloadResult")
    @ResponseBody
    @ApiOperation(value = "7.下载排刀结果", httpMethod = "POST")
    public ResponseEntity<InputStreamResource> downloadResult(@RequestParam(value = "token")String token) throws Exception {
        Integer userId = TokenUtil.explainToken(token);
        File f = new File(userId + "resultFinal.csv");
        if(!f.exists()){
            f = new File(userId + "resultTemp.csv");
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename=" +
                URLEncoder.encode(userId + "result.csv","UTF-8"));
        FileInputStream in = new FileInputStream(f);

        return ResponseEntity
                .ok()
                .headers(headers)
                .body(new InputStreamResource(in));
    }
}
